package com.study.news.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.study.news.pojo.Comment;
import com.study.news.utils.BaseDAO;
import com.study.news.utils.RowMapper;

public class CommentDao extends BaseDAO {

	public List<Comment> getCommentList(int newsId) {
		List<Comment> commentList = super.query("SELECT * FROM comment where newsid=? order by id desc", new Integer[] {newsId},
				new RowMapper<Comment>() {
					@Override
					public Comment rowmap(ResultSet rs) throws SQLException {
						Comment comment = new Comment();
						comment.setId(rs.getInt("id"));
						comment.setAuthor(rs.getString("author"));
						comment.setCommentDate(rs.getTimestamp("commentDate"));
						comment.setContent(rs.getString("content"));
						return comment;
					}
				});

		return commentList;
	}
	
	public int addComment(Comment comment) {
		return super.insert("INSERT INTO comment(newsid,content,author)VALUES(?,?,?)", new  Object[] 
				{comment.getNewsid(), comment.getContent(), comment.getAuthor()});
		
	}

}
